package com.jh3502.vivo.function.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.base.BasePresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author harisucici
 * @version 2018/12/1 16:10
 * name
 * todo
 * @email harisucici@gmail.com
 */
public class DialogList extends BaseActivity {
    @BindView(R.id.dialog_choice_list)
    ListView dialogChoiceList;
    @BindView(R.id.dialog_choice_title)
    TextView dialogChoiceTitle;
    private int chosen = -1;

    @Override
    public BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected int setViewId() {
        return R.layout.dialog_choice;
    }

    @Override
    protected void initView() {
        dialogChoiceTitle.setText(getIntent().getStringExtra("title"));
        String[] datas = getIntent().getStringArrayExtra("list");
        ListChoiceAdapter adapter = new ListChoiceAdapter(this, datas);
        dialogChoiceList.setAdapter(adapter);
        dialogChoiceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                chosen = position;
                adapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {

    }

    @OnClick(R.id.dialog_choice_cancel)
    public void onDialogChoiceCancelClicked() {

        onBackPressed();
    }

    @OnClick(R.id.dialog_choice_submit)
    public void onDialogChoiceSubmitClicked() {
        if (chosen == -1) {
            showShortToast("最少选择一项");
            return;
        }
        Intent intent = new Intent();
        intent.putExtra("chosen", chosen);
        intent.putExtra("positionMain", getIntent().getIntExtra("positionMain", -1));
        intent.putExtra("typeItem", getIntent().getIntExtra("typeItem", -1));
        intent.putExtra("positionItem", getIntent().getIntExtra("positionItem", -1));
        setResult(Activity.RESULT_OK, intent);
        onBackPressed();
    }

    private class ListChoiceAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private String[] list;

        public ListChoiceAdapter(Context context, String[] list) {
            this.inflater = LayoutInflater.from(context);
            this.list = list;


        }

        @Override
        public int getCount() {
            return list.length;
        }

        @Override
        public Object getItem(int position) {
            return list[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ListChoiceAdapter.ViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_list_choice, null);
                holder = new ListChoiceAdapter.ViewHolder();
                holder.item_list_choice_check = convertView.findViewById(R.id.item_list_choice_check);
                holder.item_list_choice_name = convertView.findViewById(R.id.item_list_choice_name);

                convertView.setTag(holder);
            } else {
                holder = (ListChoiceAdapter.ViewHolder) convertView.getTag();
            }
            boolean check = ((ListView) parent).isItemChecked(position);
            holder.item_list_choice_check.setChecked(check);
            holder.item_list_choice_name.setText(list[position]);


            return convertView;
        }

        private class ViewHolder {
            CheckBox item_list_choice_check;
            TextView item_list_choice_name;

        }
    }
}
